<?php
class Menu_IndexController extends Zend_Controller_Action
{
	private $menu;	
	public function indexAction(){
		
		$data= new Menu_Models_Business_Menu();
		$arr=$data->getMenu();		
		 
		foreach ($arr as $key=>$value){
			$this->menu[$value["parent"]][] = $value;
		}
			
		$tree = array();
		foreach ($this->menu[0] as $key=>$value){
			$objTree = new stdClass;
			$objTree->id = $value["id"];
			$objTree->text = html_entity_decode($value["display_name"]);
			$objTree->cls = 'folder';			
			$objTree->singleClickExpand = true;
			
			$objTree->children=$this->subMenu($value["id"]);
			$tree[]= $objTree;
		}		
		echo json_encode($tree);
		$this->_helper->layout->disableLayout();		    
	}
	
	private function subMenu($id)
	{
		if (@$this->menu[$id])
		{
			$children = array();
			foreach ($this->menu[$id] as $key=>$value){
				$obj = new stdClass;
				$obj->text = html_entity_decode($value["display_name"]);				

				//forder
				if (@$this->menu[$value["id"]]){
					$obj->id = $value["id"];
					$obj->cls = 'folders';
					//$obj->iconCls = 'icon-arrow-north';
					$obj->leaf = false;
					$obj->children=$this->subMenu($value["id"]);	
				}else {
					//link
					$obj->id = $value["module_name"]."_".$value["controller_name"]."_".$value["action_name"];
					$obj->cls = 'file';
					$obj->leaf = true;	
					//$obj->iconCls = 'icon-arrow-north';
					$url = '/'.$value["module_name"];
					$url.=($value["controller_name"]=='index')
							? '/'
							:($value["action_name"]=='index')
								?'/'.$value["controller_name"].'/'
								:'/'.$value["controller_name"].'/'.$value["acrion_name"].'/';
					
					$obj->href = $url;
				}		
					
				$children[] = $obj;
			}
			return $children;
		}
	} 
}

